//
//  DropDownMenu2.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/7/8.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "DropDownMenu2.h"
#import "DropDownMenuCell.h"
#define CELLHEIGHT 44
#define CELLWITH 120
static DropDownMenu2 * dropDownObj = nil;


@implementation DropDownMenu2
{
    UILabel *textLabel;
    UIImageView *view;
}
@synthesize table;
@synthesize senderTag;
@synthesize delegate;
@synthesize isDisplay;
@synthesize selectedTitle;
- (void) setDropDownMenuParameterWithcellContents:(NSArray *)contentsArray
{
    cellContents = contentsArray;
    [self setUpList];
}
- (void) setDropDownMenuParameterWithcellContents:(NSArray *)contentsArray andWidth:(CGFloat)dropViewWithd{
    cellWidth=dropViewWithd;
    cellContents = contentsArray;
    [self setUpList];
}

#pragma mark -单例方法
+ (DropDownMenu2 *)shareDropDownMenu
{
    if (dropDownObj == nil)
    {
        dropDownObj = [[self alloc] init];
    }
    return dropDownObj;
}

- (id)init
{
    self = [super init];
    if (self) {
        
        
    }
    return self;
}

- (void)setUpList
{
    self.frame = CGRectMake(0, 0,SCREENWIDTH, SCREENHEIGH);
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"toumingbg"]];
    self.layer.masksToBounds = NO;

    
    tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(packUpList)];
    tap.delegate=self;
    [self addGestureRecognizer:tap];
    
    view = [[UIImageView alloc] init];
    
    if (_backViewFlag ==1 && cellWidth==130) {
        
        view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_one_"]];
    }else if(_backViewFlag ==2 && cellWidth==120)
    {
        view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_two_"]];
    }else if (_backViewFlag ==3 && cellWidth==120)
    {
        view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_three_"]];
    }else if (cellWidth==125)
    {
        if (_middleFlag) {
            
            
            if (cellContents.count ==1)
            {
                
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125onecenter_"]];
                
            }else if (cellContents.count ==2)
            {
                
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125twocenter_"]];
                
            }else if (cellContents.count ==3)
            {
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125threecenter_"]];
                
            }else
            {
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125fourcenter_"]];
            }

        }else
        {
            if (cellContents.count == 1) {
                
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125one_"]];
            }else if (cellContents.count ==2)
            {
                
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125two_"]];
                
            }else if (cellContents.count ==3)
            {
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_125three_"]];
                
            }else
            {
                view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listdrop_radio_four_"]];
            }
            

        }
        
    }else if (cellWidth == 74)
    {
        view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"hdgc_list_74_"]];
    }
    
    
    if (_middleFlag) {
        
    view.frame =CGRectMake(SCREENWIDTH/2-cellWidth/2, btn.origin.y+btn.size.height+5, cellWidth, 0);
        
    }else
    {
    view.frame =CGRectMake(btn.origin.x, btn.origin.y+btn.size.height+5, cellWidth, 0);
    }
    
    [self addSubview:view];
    
    if (_middleFlag) {
        
    table = [[UITableView alloc] initWithFrame:CGRectMake(SCREENWIDTH/2-cellWidth/2, btn.origin.y+btn.size.height+15, cellWidth, 0)];
        
    }else
    {
    table = [[UITableView alloc] initWithFrame:CGRectMake(btn.origin.x, btn.origin.y+btn.size.height+15, cellWidth, 0)];
    }
    
    
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.backgroundColor=[UIColor whiteColor];
    table.showsVerticalScrollIndicator = NO;
    table.layer.cornerRadius =4.;
    [self addSubview:table];
    
}

- (void)hidden{
    if (isDisplay)
    {
        self.isDisplay = NO;
        CGRect selfFrame = self.frame;
        self.frame = CGRectMake(selfFrame.origin.x ,selfFrame.origin.y, self.frame.size.width, 0);
        view.frame = CGRectMake(btn.origin.x+btn.size.width-cellWidth,btn.origin.y + btn.size.height ,cellWidth , 0);
        table.frame = CGRectMake(btn.origin.x+btn.size.width-cellWidth,btn.origin.y + btn.size.height+9 ,cellWidth , 0);
    }
}

-(void)hideDropDown:(UIView *)b
{
    NSObject *objec = (NSObject *)delegate;
    if (b!= nil)
    {
        
        if ([objec isKindOfClass:[UIViewController class]]) {
            UIViewController * controller = (UIViewController *)delegate;
            // 将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
            btn  = [b convertRect:b.bounds toView:controller.view];
        }
        else if ([objec isKindOfClass:[UIView class]]){
            btn  = [b convertRect:b.bounds toView:(UIView *)delegate];
        }
        if (btn.origin.y==-44) {
            _flag=NO;
        }
        
    }
    
    NSInteger droplistX;
    
    if (_middleFlag) {
        droplistX=SCREENWIDTH/2-cellWidth/2;
        
    }else
    {
        droplistX=btn.origin.x+btn.size.width-cellWidth;
    }
//        if (btn.origin.x + btn.size.width/2 < SCREENWIDTH/2) {
//    
//            if (btn.origin.x + self.frame.size.width < SCREENWIDTH)
//            {
//                droplistX = btn.origin.x;
//            }
//            else
//            {
//                droplistX = btn.origin.x - (SCREENWIDTH - (btn.origin.x + self.frame.size.width));
//            }
//        }
//        else
//        {
//            if (btn.origin.x + btn.size.width > self.frame.size.width)
//            {
//                droplistX = btn.origin.x + btn.size.width - self.frame.size.width;
//            }
//            else
//            {
//                droplistX = 0;
//            }
//        }
    
    //isDisplay YES 收回 NO展示
    if (isDisplay)
    {
        self.isDisplay = NO;
        CGRect selfFrame = self.frame;
        self.frame = CGRectMake(selfFrame.origin.x ,selfFrame.origin.y, self.frame.size.width, 0);
        if (_flag) {
        
        view.frame = CGRectMake(droplistX,btn.origin.y + btn.size.height ,cellWidth , 0);
        table.frame = CGRectMake(droplistX,btn.origin.y + btn.size.height+9 ,cellWidth , 0);
            
        }else
        {
        view.frame = CGRectMake(droplistX,btn.origin.y + btn.size.height+SafeAreaTopHeight ,cellWidth , 0);

        table.frame = CGRectMake(droplistX,btn.origin.y + btn.size.height+SafeAreaTopHeight+9 ,cellWidth , 0);
        }
    }
    else
    {
      
        self.isDisplay = YES;
        
        if (CELLHEIGHT * [cellContents count]+9 > 185) {
            self.frame = CGRectMake(0,0 ,self.frame.size.width, SCREENHEIGH);
            if (_flag) {
            
            view.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height, cellWidth, 185);
            table.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height+9, cellWidth, 176);
            }else
            {
            view.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height+SafeAreaTopHeight, cellWidth, 185);
            table.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height+SafeAreaTopHeight+9, cellWidth, 176);
            }
        }
        
        else
        {
            /**
             *  编辑人：张东海
             *  时间：2016年5月25日
             *  更改或添加内容：self.frme的高由20改成了10
             *  备注：太丑了，看不下去了！
             */
            //高度加5
             self.frame = CGRectMake(0, 0,self.frame.size.width,SCREENHEIGH);
            if (_flag) {
                
            view.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height, cellWidth, CELLHEIGHT*[cellContents count]+9);
             table.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height+9, cellWidth, CELLHEIGHT*[cellContents count]);
            }else
            {
            view.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height+SafeAreaTopHeight, cellWidth, CELLHEIGHT*[cellContents count]+9);
            table.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height+SafeAreaTopHeight+9, cellWidth, CELLHEIGHT*[cellContents count]);
            }
        }
        
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return CELLHEIGHT;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cellContents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
    
    DropDownMenuCell *cell;
    if (tableView  == table) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       if (cell == nil)
       {
        cell = [[DropDownMenuCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
    }
    cell.backgroundColor=[UIColor clearColor];
    
    
//    if (indexPath.row==0) {
//        
//        cell.centerLabel.frame=CGRectMake(15, 22, cellWidth-30, 15);
//    }else
//    {
        cell.centerLabel.frame=CGRectMake(15, 13, cellWidth-30,15);
//    }
    cell.centerLabel.tag = indexPath.row+100;
    cell.centerLabel.font = [UIFont systemFontOfSize:15];
    cell.centerLabel.textAlignment = NSTextAlignmentCenter;
    cell.centerLabel.textColor=[UIColor colorWithHexString:@"39adff"];
    if (indexPath.row < cellContents.count) {
        
        cell.centerLabel.text = [cellContents objectAtIndex:indexPath.row];
    }else{
        cell.centerLabel.text = @"确定";
    }

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    UIView *line=[[UIView alloc]init ];
//    if (indexPath.row==0) {
//        line.frame=CGRectMake(0, 52, cellWidth, 1);
//    }else
//    {
        line.frame=CGRectMake(0, 43, cellWidth, 1);
//    }
    
    line.backgroundColor=[UIColor colorWithHexString:@"DEDEDE"];
    [cell.contentView addSubview:line];
    if (indexPath.row==cellContents.count-1) {
        line.hidden=YES;
    }

    return cell;
}

//选择表格事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    
    selectedTitle = [cellContents objectAtIndex:indexPath.row];
    //设置tag值，用于代理方法里面传值使用
    self.senderTag = indexPath.row;
    
    //代理方法
    [delegate niDropDownDelegateMethod:self];
    
    //收回下拉的tableView表格
    [self packUpList];
    
}

#pragma mark -收回下拉的表格
- (void)packUpList
{
    if (isDisplay)
    {
        CGRect selfFrame = self.frame;
        self.isDisplay = NO;
        self.frame = CGRectMake(selfFrame.origin.x ,selfFrame.origin.y, self.frame.size.width, 0);
        view.frame = CGRectMake(btn.origin.x ,btn.origin.y+btn.size.height, 0, 0);
        table.frame = CGRectMake(btn.origin.x ,btn.origin.y+btn.size.height+9, 0, 0);
        
    }
    
    if (delegate && [delegate respondsToSelector:@selector(taptoRemoveViewWithSenderTag:)]) {
        [delegate taptoRemoveViewWithSenderTag:self.tag];
    }
        
    
    //if (_needDelegate == YES) {
        
        //[delegate taptoRemoveViewWithSenderTag:self.tag];
        //_needDelegate = NO;
    //}
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
    
    if ([touch.view isKindOfClass:[UIControl class]]) {
        
        return YES;
    }
    
    return NO;
    
}
@end
